package com.zxf.basic.view

import android.R
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration


/**
 * @author zjc
 * @date 2022/12/30
 *
 * https://liarrdev.github.io/post/Setting-Divider-for-RecyclerView-in-Android/
 */
class DividerItemDecoration(context: Context) : ItemDecoration() {
    var marginEnd: Int = 0
    var marginStart: Int = 0
    var drawable: Drawable? = null
    var orientation = LinearLayoutManager.VERTICAL

    init {
        if (drawable == null) {
            val a: TypedArray = context.obtainStyledAttributes(ATTRS)
            drawable = a.getDrawable(0)
            if (drawable == null) {
                Log.w(TAG,
                    "@android:attr/listDivider was not set in the theme used for this " + "DividerItemDecoration. Please set that attribute all call setDrawable()")
            }
            a.recycle()
        }
        checkOrientation(orientation)
    }

    private fun checkOrientation(orientation: Int) {
        require(!(orientation != HORIZONTAL && orientation != VERTICAL)) { "Invalid orientation. It should be either HORIZONTAL or VERTICAL" }
    }

    override fun onDraw(
        @NonNull c: Canvas,
        @NonNull parent: RecyclerView,
        @NonNull state: RecyclerView.State,
    ) {
        if (parent.layoutManager == null) {
            return
        }
        if (orientation == VERTICAL) {
            drawVertical(c, parent)
        } else {
            drawHorizontal(c, parent)
        }
    }

    private fun drawVertical(canvas: Canvas, parent: RecyclerView) {
        val left = parent.paddingLeft
        val right = parent.width - parent.paddingRight
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child: View = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            val top: Int = child.bottom + params.bottomMargin
            val bottom: Int = top + drawable!!.intrinsicHeight
            drawable!!.setBounds(left + marginStart, top, right - marginEnd, bottom)
            drawable!!.draw(canvas)
        }
    }

    private fun drawHorizontal(canvas: Canvas, parent: RecyclerView) {
        val top = parent.paddingTop
        val bottom = parent.height - parent.paddingBottom
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child: View = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            val left: Int = child.right + params.rightMargin
            val right: Int = left + drawable!!.intrinsicHeight
            drawable!!.setBounds(left, top + marginStart, right, bottom - marginEnd)
            drawable!!.draw(canvas)
        }
    }

    override fun getItemOffsets(
        @NonNull outRect: Rect,
        @NonNull view: View,
        @NonNull parent: RecyclerView,
        @NonNull state: RecyclerView.State,
    ) {
        if (orientation == VERTICAL) {
            outRect.set(0, 0, 0, drawable!!.intrinsicHeight)
        } else {
            outRect.set(0, 0, drawable!!.intrinsicWidth, 0)
        }
    }

    companion object {
        private val ATTRS = intArrayOf(R.attr.listDivider)
        const val HORIZONTAL = LinearLayoutManager.HORIZONTAL
        const val VERTICAL = LinearLayoutManager.VERTICAL
        private const val TAG = "DividerItem"
    }

}
